package com.xinfu.newproject.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.xinfu.newproject.R;
import com.xinfu.newproject.base.BaseActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WebPayActivity extends BaseActivity {

    @BindView(R.id.activity_web_pay_web)
    WebView webview;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_pay);
        ButterKnife.bind(this);
        registerBack();
        Bundle bundle = getIntent().getExtras();
        setActivityTitle(bundle.getString("title"));
        initViewBind();
        initLisener();
    }

    private void initLisener() {

    }

    private void initViewBind() {
        setActivityTitle("支付");
        Map<String, String> map = new HashMap<>();
        map.put("Referer", "http://....");
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");
        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webview.setWebChromeClient(new WebChromeClient());
        webview.setWebViewClient(new WebViewClient() {
            String referer = "商户申请H5时提交的授权域名";
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 获取上下文, H5PayDemoActivity为当前页面
                final Activity context = WebPayActivity.this;
//                String url = request.getUrl().toString();
                // ------ 对alipays:相关的scheme处理 -------
                if (url.startsWith("alipays:") || url.startsWith("alipay")) {
                    try {
                        context.startActivity(new
                                Intent("android.intent.action.VIEW", Uri.parse(url)));
                    } catch (Exception e) {
                        new AlertDialog.Builder(context)
                                .setMessage("未检测到支付宝客户端，请安装后重试。")
                                .setPositiveButton("立即安装", new
                                        DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                Uri alipayUrl = Uri.parse("https://d.alipay.com");
                                                context.startActivity(new
                                                        Intent("android.intent.action.VIEW", alipayUrl));
                                            }
                                        }).setNegativeButton("取消", null).show();
                    }
                    return true;
                } else if (url.startsWith("weixin://wap/pay?")) {//打开微信进行支付
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    context.startActivity(intent);
                    return true;
                } else {
                    Map<String, String> extraHeaders = new HashMap<String, String>();
                    //如果是调起微信支付，需要在webview中手动设置referer
                    //extraHeaders.put("Referer", "https://yaoyaotest.cebbank.com"); //测试环境
                    extraHeaders.put("Referer", "https://yaoyao.cebbank.com");//生产环境
                    view.loadUrl(url, extraHeaders);

                    return true;
                }
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                // TODO Auto-generated method stub
                handler.proceed();
            }
        });
        webview.loadUrl(getIntent().getExtras().getString("response"));

    }
}
